<?php
namespace app\index\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\Cache;
class Base extends BaseController
{
    /*定义一个初始数组*/
    protected $info = array(
        'code'=>200,
        'msg'=>'提示',
        'total'=>0,//数据数量
        'data'=>[]
    );

    /**
     * 限制某ip一定时间内请求次数(防恶意请求接口)
     * 正常请求返回true,超过请求次数的时间返回false
     * @param int $num 超过3次不让访问(这里没写错,就是超过3次,不是2次)
     * @param int $time 禁止访问后,距离允许下次请求的间隔时间(单位：秒,这里默认是3秒内不允许再次请求)
     * @return bool
     */
    protected function getResult($num = 2, $time = 60){
        $ip = request()->ip();    //获取来源ip
        $path = request()->url(); //获取当前地址
        $url = $ip . $path;       //拿ip和地址拼接成key
        // Cache::delete($url); 
        $res = Cache::get($url);  //获取value
        if ($res) {               //如果有value(说明之前已经请求过了)
            if ($res > $num) {    //超过规定请求的次数
                return false;
            }
            Cache::inc($url, 1);    //没超过规定请求的次数,次数自增1
            return true;
        } else {
            Cache::set($url, 1, $time); //初次请求,写入缓存与有效时间
            return true;
        }
    }


    //系统配置表 json数据格式化
    protected function formatConfig($key){
        $data = Db::table('an_config')
        ->where('key',$key)
        ->find();
        if($data['arr'] == 'y' || $data['data_type'] == 'img' ){
            $data['value'] = json_decode($data['value'],true); 
        }
        return $data;
    }

    //根据页面ID 获取页面信息
    protected function getPageData($id){
        $pageData = Db::table('an_page')
        ->where('a_show','1')
        ->where('id',$id)
        ->find();
        $this->assign('pageData',$pageData);
    }
    
    protected function initialize()
    {
        //获取数据库里面的配置，赋值到所有模板
        $c = Db::table('an_config')
        ->whereOr('tabs_type','web_config')
        ->whereOr('tabs_type','web_seo')
        ->whereOr('tabs_type','company')
        ->select()
        ->toArray(); 

        $arr = [];
        foreach($c as $k => $v){
            if($v['arr'] == 'y' || $v['data_type'] == 'img' ){
                $v['value'] = json_decode($v['value'],true);
            }
            $arr[$v['key']] = $v['value'];
        }
        // dump($arr['web_config_dev_plan']);
        $this->assign('c',$arr);

        //获取产品分类 菜单
        $goodsType = Db::table('an_type')->where('type','goods')->select()->toArray(); 
        foreach($goodsType as $k=>$v){
            $goodsType[$k]['cn_name'] = $v['name'];
            $goodsType[$k]['url'] = 'index/product';
            $goodsType[$k]['jump'] = '';
        }

        //获取新闻分类 菜单
        $newsType = Db::table('an_type')->where('type','news')->select()->toArray(); 
        foreach($newsType as $k=>$v){
            $newsType[$k]['cn_name'] = $v['name'];
            $newsType[$k]['url'] = 'index/news';
            $newsType[$k]['jump'] = '';
        }

        //获取所有菜单
        $menuData = Db::table('an_page')
        ->where('a_show','1')
        ->where('type_id',1)
        ->order('id dsc')
        ->select()
        ->toArray(); 

        $navDatas = [];
        foreach($menuData as $k=>$v){
            //筛选一级菜单
            if($v['pid'] == 0){
                $menuData[$k]['sub'] = [];

                //插入产品分类 二级菜单
                if($v['url'] == 'index/product'){
                    $menuData[$k]['sub'] = $goodsType;
                }

                //插入新闻分类 二级菜单
                if($v['url'] == 'index/news'){
                    $menuData[$k]['sub'] = $newsType;
                }

                //筛选其他二级菜单
                foreach($menuData as $key=>$val){
                    if($v['id'] == $val['pid']){
                        array_push($menuData[$k]['sub'],$val);
                    }
                }
                array_push($navDatas,$menuData[$k]);
            }
        }
        $this->assign('navDatas',$navDatas);

        $gt = input('get.');
        $this->assign('getData',$gt);
    }
}
